<?php
// +----------------------------------------------------------------------
// | 多客怪兽框架多平台开发框架
// +----------------------------------------------------------------------
// | Copyright (c) 2021~2024 http://www.dkyx.cc All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 刘泓宾（514660094@qq.com）
// +----------------------------------------------------------------------

namespace app\controller\adminapi\platform\wechat;

use app\common\services\table\DataForm;
use app\common\services\table\DataTable;
use app\common\model\wechat\WechatOrder;
use app\common\controller\PlatformController;

/**
 * TODO 微信用户订单控制器
 * Create by :  刘泓宾<514660094@qq.com>
 * Copyright :  copyright (c) 多客怪兽, www.dkyx.cc
 * Created on:  2025/5/13 20:49
 */
class Order extends PlatformController
{
    /**
     * @var WechatOrder
     */
    protected $model;


    // 设置关联查询
    protected array $withJoinTable = [];

    // 设置快捷搜索字段
    protected string|array $quickSearchField = ['out_trade_no', 'transaction_id'];

    /**
     * 控制器初始化
     * Create by :  刘泓宾<514660094@qq.com>
     * @return void
     */
    public function initialize(): void
    {
        parent::initialize();
        $this->model = new WechatOrder();
        $this->model->plaid = get_plaid();
        // 判断请求控制器方法
        if ($this->request->action() == "index" && !$this->request->param("select")) {
            // 定义全局的查询范围带有alias
            $this->model->setGlobalScope();
        }
    }

    /**
     * 数据表格
     * Create by :  刘泓宾<514660094@qq.com>
     * @return \think\response\Json
     */
    public function table()
    {
        $DataTable = (new DataTable($this->model));
        if ($this->request->param('recycle')) {
            // 回收站
            $DataTable->field([$this->model->getPk(), 'delete_time']);
        }
        $column = $DataTable
            ->setHideFields(['update_time'])
            ->setField('openid', ['render' => 'copy'])
            ->setAfterFields([
                'topuid' => ['slotName' => 'addons_solt', 'label' => '应用', 'prop' => 'addons']
            ])
            ->operation([], ['edit', 'del'])->create();
        return $this->success('ok', ['column' => $column, 'pk' => $this->model->getPk()]);
    }

    /**
     * 数据表单
     * Create by :  刘泓宾<514660094@qq.com>
     * @return \think\response\Json
     */
    public function form()
    {
        $dataForm = new DataForm($this->model);
        $form = $dataForm
            ->setFilterFields(['sex', 'language', 'city', 'province', 'country', 'unionid', 'first_addon', 'last_addon', 'groupid', 'update_time', 'relation_type'])
            ->create();
        return $this->success('', $form);
    }

    /**
     * 获取数据列表
     * Create by :  刘泓宾<514660094@qq.com>
     * @return \think\response\Json
     * @throws \Throwable
     * @throws \think\db\exception\DbException
     */
    public function index()
    {
        if ($this->request->param('select')) {
            return $this->select();
        }
        list($where, $alias, $limit, $order) = $this->queryBuilder();
        $res = $this->model
            ->field($this->indexField)
            ->withJoin($this->withJoinTable, $this->withJoinType)
            ->alias($alias)
            ->where($where)
            ->order($order)
            ->paginate($limit);
        return $this->success('', [
            'list'  => $res->items(),
            'total' => $res->total(),
        ]);
    }
}
